package com.caogen.cg_wl_kt.api

import android.app.Activity
import com.caogen.cg_wl_kt.base.BaseP
import com.caogen.cg_wl_kt.base.Gl
import com.caogen.cg_wl_kt.model.UserBean
import com.caogen.cg_wl_kt.net.HttpApi
import com.caogen.cg_wl_kt.net.HttpApi.await
import com.caogen.cg_wl_kt.net.HttpRes
import com.caogen.cg_wl_kt.utils.YDLog
import com.google.gson.JsonObject
import kotlinx.coroutines.launch

/**
 * 项目名称: cg_wl_kt
 * Packages:com.caogen.cg_wl_kt.api
 * author: FSW
 * 创建日期: 2024/2/26 14:10
 * Describe:
 *
 */
object LoginApi:BaseP() {
    // 登录
    fun reqLogin(a: Activity, params: MutableMap<String, String>, callback: OnBeanCallBack) {
        HttpApi.io.launch {
            try {
                val map = toMapRequestBody(params)
                val res: HttpRes<JsonObject> =
                    HttpApi.api.login(map).await()
                ui(a) {
                    if (res.code == "1") {
                        val userinfo = res.data?.get("userinfo");
                        val data: UserBean? = Gl.getStringToBean(userinfo,UserBean::class.java)
                        callback.onSuccess(data)
                    } else {
                        toast(res.msg ?: "")
                        callback.onError(null)
                    }
                }
            } catch (e: Exception) {
                loge("reqCompanyList.数据异常.${e.cause}.${e.message}")
                callback.onError(null)
            }
        }
    }
}